import gdata.youtube as youtube
import gdata.youtube.service as yservice
import RandomPhrase
import random

class YouTubeVideo():
	def __init__(self, video_entry):
		self.video_entry = video_entry
		
	def __getattr__(self, attr):
		try:
			return self.__dict__[attr]
		except:
			return getattr(self.video_entry, attr)
		
	def __setattr__(self, attr, val):
		try:
			self.__dict__[attr] = val
		except:
			setattr(self.video_entry, attr, val)
			
	def __get_content_grades(self):
		pass
		
	def __get_frame_grades(self):
		pass
	

class YouTubeFetcher(object):
	def __init__(self):
		self.yt_service = yservice.YouTubeService()

	def __next__(self):
		sortby = random.choice(self.SORT_BY)
		
		keyword = "%s"%RandomPhrase.RandomPhrase()
		
		query = yservice.YouTubeVideoQuery()
		query.vq = keyword
		query.orderby = sortby
		query.racy = 'exclude'
		feed = self.yt_service.YouTubeQuery(query)
		try:
			return YouTubeVideo(random.choice(feed.entry))
		except IndexError:
			return None

	def __iter__(self):
		return self
	
	SORT_BY = ['relevance', 'published', 'viewCount', 'rating']		#['Relevance', 'Newest', 'Oldest', 'View Count', 'Rating']

def main():
	yc = YouTubeFetcher()
	print yc.__next__().comments
	
if "__main__" == __name__:
	main()